from playsound import playsound
from aip import AipSpeech

def play(data):
    """ 百度APPID AK SK """
    APP_ID = '25327913'
    API_KEY = 'pf0WYON8UYYeuGW8i0I0Ld8R'
    SECRET_KEY = 'jaVBwMxSlkvhmxH5FGNELDPvBZDGgbYZ'
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

    # tts = '君不见，黄河之水天上来，奔流到海不复回。'
    # 合成的文本，使用UTF-8编码， 请注意文本长度必须小于1024字节

    voiceresult = client.synthesis(data, 'zh', 1, {'pit':3, 'vol': 5, 'per': 3})

    # # 发音人选择, 0为女声，1为男声，
    # # 3为情感合成-度逍遥，4为情感合成-度丫丫，默认为普通女
    # spd：语速，取值0-9，默认为5中语速
    # pit：音调，取值0-9，默认为5中语调
    # vol：音量，取值0-15，默认为5中音量
    # per：发音人选择, 0为女声，1为男声， 3为情感合成-度逍遥，4为情感合成-度丫丫，默认为普通女
    save_file = u'1426.mp3'

    # with open(save_file, 'wb') as of:
    #     of.write(voiceresult)
    #     of.close()

    if not isinstance(voiceresult, dict):
        with open(save_file, "wb") as f:
            f.write(voiceresult)
        f.close()
    print('speaking...')
    print(data)
    # file ="D://python_workspace//Final//1426.mp3"
    file='D://python_workspace//Final//1426.mp3'
    playsound(file)


